package com.yl.common.ui;

import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.yl.common.utils.LogUtil;
import com.yl.common.utils.NavigationBarUtil;
import com.yl.common.utils.ToastUtil;

/**
 * Created by chenjinxu on 2019/5/26.
 * 说明:
 */
public abstract class BaseDialogFragment extends DialogFragment {
    public boolean saveStatusLog = true;
    boolean isFirst = true;
    boolean canceledOnTouchOutside = true;
    public String TAG = "TAG";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mContentView = inflater.inflate(getViewId(), container, false);
        if (saveStatusLog) {
            LogUtil.getInstance().log(TAG, "--onCreateView " + this);
        }
        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
            getDialog().setCanceledOnTouchOutside(canceledOnTouchOutside);
            NavigationBarUtil.focusNotAle(getDialog().getWindow());
            getDialog().setOnShowListener(dialog -> {
                if (getDialog() != null && getDialog().getWindow() != null) {
                    NavigationBarUtil.hideNavigationBar(getDialog().getWindow());
                    NavigationBarUtil.clearFocusNotAle(getDialog().getWindow());
                }
            });
        }
        return mContentView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFirst = true;
        if (saveStatusLog) {
            LogUtil.getInstance().log(TAG, "--onDestroyView " + this);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (getDialog() != null) {
            Window window = getDialog().getWindow();
            if (window == null) {
                return;
            }
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            WindowManager manager = window.getWindowManager();
            DisplayMetrics metrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(metrics);

            WindowManager.LayoutParams attributes = window.getAttributes();
            int width = (int) (metrics.widthPixels * getDialogWidth());
            attributes.width = width == 0 ? WindowManager.LayoutParams.WRAP_CONTENT : width;
            window.setAttributes(attributes);
        }
    }

    public BaseDialogFragment setCanceledOnTouchOutside(boolean cancel) {
        canceledOnTouchOutside = cancel;
        return this;
    }

    public void showToast(String msg) {
        if (msg == null || getContext() == null) {
            return;
        }
        ToastUtil.getInstance().showText(msg);
    }

    public void showLoading(String title) {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) getActivity();
            baseActivity.showLoading(title);
        }
    }

    public void hideLoading() {
        if (getActivity() != null && getActivity() instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) getActivity();
            baseActivity.hideLoading();
        }
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T findViewById(int id) {
        T result = getView().findViewById(id);
        if (result == null) {
            throw new IllegalArgumentException("view 0x" + Integer.toHexString(id) + "doesn't exist");
        }

        return result;
    }

    public abstract int getViewId();

    protected float getDialogWidth() {
        return getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? 0.5f : 0.8f;
    }

}
